package com.boot.controller;

import com.boot.entity.User;
import com.boot.mapper.UserMapper;
import jakarta.annotation.Resource;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.Collections;

/**
 * 登录控制器
 *
 * @author ljk
 * @since 2024/3/14 22:11
 */
@Controller
public class LoginController {
    @Setter(onMethod_ = @Autowired)
    private PasswordEncoder passwordEncoder;
    @Setter(onMethod_ = @Resource)
    private UserMapper userMapper;

    @GetMapping("/login")
    public String login() {
        return "login";
    }


    @GetMapping("/register")
    public String register() {
        return "register";
    }

    @PostMapping("/user/save")
    public String userSave(String username, String password) {

        User user = new User();
        user.setUsername(username);
        user.setPassword(passwordEncoder.encode(password));
        user.setEnabled(true);
        user.setAuthorities(Collections.emptyList());
        userMapper.insert(user);

        return "redirect:/login";
    }

}
